# -*- coding: utf-8 -*-

# graphic-pppoe-client - Easy graphic networking manager
#
# Author: GoTLiuM InSPiRiT <gotlium@gmail.com>
#
# Copyright 2010, GoTLiuM InSPiRiT <gotlium@gmail.com>.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

from helpers.connections import Connections
from helpers.routes import Routes
from helpers.users import Users
from helpers.ipconfigs import Ipconfigs
from helpers.firewall import Firewall
from models.base import BaseModel
from libraries.net import setIfState, getIfState
from models.__init__ import getIfnameByConnId
from time import sleep

class Wired(BaseModel, Connections, Users, Routes, Ipconfigs, Firewall):

	def checkConnection(self, connId):
		ifName = getIfnameByConnId(connId)
		if getIfState(ifName):
			return True
		return False

	def upConnection(self, connId):
		ifName = getIfnameByConnId(connId)
		if ifName:
			setIfState(ifName, 0)
			sleep(1)
			setIfState(ifName, 1)
			sleep(1)
			if self.setIpconfig(connId):
				if self.setRoutes(connId):
					sleep(1)
					if getIfState(ifName):
						self.setFwRules(connId)
						self.setUsers(connId)
						return True
		return False

	def downConnection(self, connId):
		self.delUsers(connId)
		self.delFwRules(connId)
		self.delRoutes(connId)
		self.delIpconfig(connId)
		return True
